<?php
/**
 *
 * Author: guorongzu
 * Date: 2022/04/06
 * Time: 15:56
 */

namespace App\Service\Core\Admin\LoginLog;

use App\Model\Core\Admin\AdminLoginLogModel;
use App\Traits\ModelStoreTrait;

/**
 * @method $this setUserId($val)
 * @method $this setUsername($val)
 * @method $this setSource($val)
 * @method $this setDevice($val)
 * @method $this setDeviceId($val)
 * @method $this setAppVersion($val)
 * @method $this setLoginIp($val)
 * @package App\Service\Core
 */
class AdminLoginLogStoreService
{
    use ModelStoreTrait;

    private array $required_param = ['user_id', 'username', 'source', 'login_ip'];

    /**
     * 添加应用
     * @return array
     */
    public function add(): array
    {
        if (!empty($this->errors)) {
            return alert_info(1, $this->errors[0], $this->errors);
        }
        $this->check_required_param($this->required_param);
        if (!empty($this->errors)) {
            return alert_info(1, $this->errors[0], $this->errors);
        }

        $data = $this->params;
        $data['login_at'] = date("Y-m-d H:i:s");
        $AdminLoginLogModel = AdminLoginLogModel::getInstance();
        $res = $AdminLoginLogModel->addLog($data);
        if ($res !== false) {
            return alert_info(0, '操作成功');
        } else {
            return alert_info(1, '操作失败');
        }
    }
}
